using System;
using System.Data;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Web;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class DataTableAndXml : System.Web.UI.Page
{
    string fileName = "";
    string connectionString = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        fileName = Server.MapPath("JSAccounts.xml");
        connectionString =
        WebConfigurationManager.ConnectionStrings[
        "RCVDB2000"].ConnectionString;
        if (!IsPostBack)
            ShowXmlData(fileName);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            SqlServerProvider sqlServerProvider =
            new SqlServerProvider(connectionString);
            sqlServerProvider.CommandText =
            "Select top 10 UserID, Email,"
            + " FirstName, LastName, Address" +
            " from tblJSAccounts";
            DataTable dataTable =
                sqlServerProvider.GetDataTable();
            
            dataTable.WriteXml(fileName, XmlWriteMode.IgnoreSchema, true);
            
        }
        catch (Exception ex)
        {
            Literal1.Text = ex.Message;
        }
    }
    void ShowXmlData(string fileName)
    {
        try
        {
            DataTable dataTable = new DataTable();
            
            dataTable.ReadXml(fileName);
            if (dataTable != null)
            {
                GridView1.DataSource = dataTable ;
                GridView1.DataBind();
            }
        }
        catch (Exception ex)
        {
            Literal1.Text = ex.Message;
        }


    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        ShowXmlData(fileName);
    }
}
